package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.Material;
import co2.pojos.PR;

@ManagedBean
@ViewScoped
public class ListPrBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<PR> list;
	private PR pr;
	private String prId;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	@PostConstruct
	public void postCon(){
		list = dao.getAllPR();
		Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		map.remove("prListBean");
	}
	
	 
	public void prosesMat(){

		 if(pr!=null){
			  try{
				  	String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
					FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/pr_master.jsf?faces-redirect=true&idpr="+pr.getId());
			  }catch(Exception e){
				  e.printStackTrace();
			  }
		 }
		
	}
	public void detail(){
		/*Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		map.remove("prFront");
		map.put("prFromList", pr);
		try{
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/pr_detail_from_list_content.jsf");
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"redirect error",""));
		}*/
		if(pr==null){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Null pointer",""));
		}
		
	}
	public void delete(){
		if(pr!=null){
			try{
				dao.delete(PR.class, pr.getId());
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Deleted!!",""));
				list = dao.getAllPR();
			}catch(Exception e){
				e.printStackTrace();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Delete Error",""));
			}
		}
	}


	public List<PR> getList() {
		return list;
	}


	public void setList(List<PR> list) {
		this.list = list;
	}


	public PR getPr() {
		return pr;
	}


	public void setPr(PR pr) {
		this.pr = pr;
	}


	public String getPrId() {
		return prId;
	}


	public void setPrId(String prId) {
		this.prId = prId;
	}


	public DAOInterface getDao() {
		return dao;
	}


	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
	
	
}
